package com.MaterialHeap

import com.MaterialHeap.location.City
import com.MaterialHeap.location.Province

class User {

	transient springSecurityService

	String username
	String password
    String nickname
    String avatar
    String phoneNumber
    String email
    Province province
    City city
    String address
    String description

	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static constraints = {
		username blank: false, unique: true
		password blank: false
        email email: true
        nickname(nullable: true)
        avatar(nullable: true)
        description(nullable: true)
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
